package com.lwj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwj.dao.ProgressMapper;
import com.lwj.dao.ProjectMapper;
import com.lwj.dao.ProjectTypeMapper;
import com.lwj.dao.TypeMapper;
import com.lwj.entity.*;
import com.lwj.model.TypeTree;
import com.lwj.model.UserProjectRes;
import com.lwj.service.ProjectService;
import com.lwj.util.DataRet;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.pkcs11.P11TlsMasterSecretGenerator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LWJ on 2018/4/26.
 */
@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectTypeMapper projectTypeMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private ProgressMapper progressMapper;

    @Override
    public DataRet<String> create(Merchant merchant,Project project) {
        project.setCreateTime(new Date());
        project.setStatus("0");
        project.setMerchantId(merchant.getId());
        projectMapper.insert(project);
        ProjectType projectType=new ProjectType();
        projectType.setProjectId(project.getId());
        projectType.setTypeId(5);
        projectTypeMapper.insert(projectType);
        return new DataRet<>("添加成功");
    }

    @Override
    public PageInfo<Project> getProject(Merchant merchant, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(projectMapper.getProject(merchant));
    }

    @Override
    public DataRet<List<TypeTree>> getTypeTree() {
        List<TypeTree> typeTrees = new ArrayList<>();
        buildTree(0, typeTrees);
        return new DataRet<>(typeTrees);
    }

    @Override
    public PageInfo<Project> getProjectByType(int pageNo, int pageSize, int typeId) {
        List<Project> projects = new ArrayList<>();
        PageHelper.startPage(pageNo, pageSize);
        if (typeId == 5) {
            projects = projectMapper.findAll();
        } else {
            projects = projectMapper.getProjectByType(typeId);
        }
        return new PageInfo<>(projects);
    }

    @Override
    public PageInfo<UserProjectRes> getUserProject(User user, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<UserProjectRes>userProjectResList=projectMapper.getUserProject(user);
        for (UserProjectRes u :
                userProjectResList) {
            if (u.getProjectStatus().equals("4")){
                u.setStatus("4");
            }
            List<Progress> progresses=progressMapper.selectByProjectId(u.getId());
            if (progresses.size()>0){
                u.setPlaneStatus("true");
                if (progresses.size()==3){
                    u.setCompleteStatus("true");
                }
            }else {
                u.setPlaneStatus("false");
            }
        }
        return new PageInfo<>(userProjectResList);
    }

    @Override
    public PageInfo<UserProjectRes> getAllProject(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<UserProjectRes>userProjectResList=projectMapper.getAllProject();
        for (UserProjectRes u :
                userProjectResList) {
            List<Progress> progresses=progressMapper.selectByProjectId(u.getId());
            if (progresses.size()>0){
                u.setPlaneStatus("true");
                if (progresses.size()==3){
                    u.setCompleteStatus("true");
                }
            }else {
                u.setPlaneStatus("false");
            }
        }
        return new PageInfo<>(userProjectResList);
    }

    @Override
    public DataRet<String> merchantDeal(int projectId) {
        Project project=projectMapper.selectByPrimaryKey(projectId);
        project.setStatus("3");
        projectMapper.updateByPrimaryKey(project);
        return new DataRet<>("确认验收完成");
    }

    @Override
    public DataRet<String> userDeal(int projectId) {
        Project project=projectMapper.selectByPrimaryKey(projectId);
        project.setStatus("2");
        projectMapper.updateByPrimaryKey(project);
        return new DataRet<>("确认验收完成");
    }

    @Override
    public DataRet<Project> getProjectDetail(int projectId) {
        return new DataRet<>(projectMapper.selectByPrimaryKey(projectId));
    }

    private void buildTree(int typeId, List<TypeTree> typeTrees) {
        List<Type> types = typeMapper.getByTypeId(typeId);
        List<TypeTree> typeTreeList = new ArrayList<>();
        for (Type t :
                types) {
            TypeTree typeTree = new TypeTree();
            typeTree.setId(t.getId());
            typeTree.setName(t.getName());
            buildTree(t.getId(), typeTreeList);
            if (typeTreeList.size() > 0) {
                typeTree.setTypeTrees(typeTreeList);
            }
            typeTrees.add(typeTree);
        }
    }
}
